package com.tdkj.action.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.information.BeanInfoModule;
import com.tdkj.model.information.BeanModuleColumn;
import com.tdkj.model.risevalue.BeanAdvert;
import com.tdkj.service.common.IBeanTypeService;
import com.tdkj.util.tree.JQueryTree;

public class TypeTreeAction extends BaseAction {

	public class JQueryTreeGoodTypeImpl implements JQueryTree {

		private String id;
		private String text;
		private String iconCls;
		private String state;
		private List<JQueryTree> children = new ArrayList<JQueryTree>();
		private Map<String, String> attributes = new HashMap<String, String>();

		public JQueryTreeGoodTypeImpl(BeanType goodType) {
			this.id = String.valueOf(goodType.getId());
			this.text = goodType.getName();
			this.attributes.put("name", goodType.getName());
			this.attributes.put("fullId", goodType.getFullId());

			if (goodType.getParent() != null) {
				this.attributes.put("parentId", String.valueOf(goodType.getParent().getId()));
			}

			if (goodType.getChildren() != null && goodType.getChildren().size() != 0) {
				for (BeanType subGoodType : goodType.getChildren()) {
					this.children.add(new JQueryTreeGoodTypeImpl(subGoodType));
				}
				this.state = "closed";
				this.iconCls = "tree-folder";
			} else {
				this.state = "open";
				this.iconCls = "tree-file";
			}
		}

		@Override
		public Map<String, String> getAttributes() {
			return attributes;
		}

		@Override
		public List<JQueryTree> getChildren() {
			return children;
		}

		@Override
		public String getId() {
			return id;
		}

		@Override
		public String getState() {
			return state;
		}

		@Override
		public String getText() {
			return text;
		}

		@Override
		public String getIconCls() {
			return iconCls;
		}
	}

	private String rootName = IBeanTypeService.Constants.ROOT_NAME_OF_GOOD;

	private JQueryTree tree = null;

	private IBeanTypeService goodTypeService;

	public String execute() {
		BeanType goodType = goodTypeService.getRootRecursive(rootName);

		if (IBeanTypeService.Constants.ROOT_NAME_OF_GOOD.equals(rootName) && (goodType.getChildren() == null || goodType.getChildren().size() == 1)) {
			BeanType 有色金属 = goodTypeService.createType(goodType.getId(), "有色金属");

			BeanType 重有色 = goodTypeService.createType(有色金属.getId(), "重有色");
			BeanType 轻有色 = goodTypeService.createType(有色金属.getId(), "轻有色");
			BeanType 贵金属 = goodTypeService.createType(有色金属.getId(), "贵金属");
			BeanType 半金属 = goodTypeService.createType(有色金属.getId(), "半金属");
			BeanType 稀土金属 = goodTypeService.createType(有色金属.getId(), "稀土金属");
			BeanType 稀有轻 = goodTypeService.createType(有色金属.getId(), "稀有轻");
			BeanType 高熔点金属 = goodTypeService.createType(有色金属.getId(), "高熔点金属");
			BeanType 放射性金属 = goodTypeService.createType(有色金属.getId(), "放射性金属");
			BeanType 稀散金属 = goodTypeService.createType(有色金属.getId(), "稀散金属");

			goodTypeService.createType(重有色.getId(), "铜");
			goodTypeService.createType(重有色.getId(), "镍");
			goodTypeService.createType(重有色.getId(), "锢");
			goodTypeService.createType(重有色.getId(), "铅");
			goodTypeService.createType(重有色.getId(), "锌");

			goodTypeService.createType(轻有色.getId(), "锑");
			goodTypeService.createType(轻有色.getId(), "汞");
			goodTypeService.createType(轻有色.getId(), "镉");
			goodTypeService.createType(轻有色.getId(), "铋");
			goodTypeService.createType(轻有色.getId(), "铝");
			goodTypeService.createType(轻有色.getId(), "镁");

			goodTypeService.createType(贵金属.getId(), "钙");
			goodTypeService.createType(贵金属.getId(), "钾");
			goodTypeService.createType(贵金属.getId(), "锶");
			goodTypeService.createType(贵金属.getId(), "钡");
			goodTypeService.createType(贵金属.getId(), "钯");
			goodTypeService.createType(贵金属.getId(), "铂");

			goodTypeService.createType(半金属.getId(), "铱");
			goodTypeService.createType(半金属.getId(), "铹");
			goodTypeService.createType(半金属.getId(), "锇");
			goodTypeService.createType(半金属.getId(), "釕");
			goodTypeService.createType(半金属.getId(), "鈹");
			goodTypeService.createType(半金属.getId(), "金");

			goodTypeService.createType(稀土金属.getId(), "银");
			goodTypeService.createType(稀土金属.getId(), "硅");
			goodTypeService.createType(稀土金属.getId(), "硒");
			goodTypeService.createType(稀土金属.getId(), "碲");
			goodTypeService.createType(稀土金属.getId(), "砷");
			goodTypeService.createType(稀土金属.getId(), "硼");

			goodTypeService.createType(稀有轻.getId(), "锂");
			goodTypeService.createType(稀有轻.getId(), "铍");
			goodTypeService.createType(稀有轻.getId(), "铷");
			goodTypeService.createType(稀有轻.getId(), "铯");
			goodTypeService.createType(稀有轻.getId(), "钨");
			goodTypeService.createType(稀有轻.getId(), "钼");
			goodTypeService.createType(稀有轻.getId(), "钽");
			goodTypeService.createType(稀有轻.getId(), "锆");
			goodTypeService.createType(稀有轻.getId(), "钒");

			goodTypeService.createType(高熔点金属.getId(), "钛");
			goodTypeService.createType(高熔点金属.getId(), "铼");
			goodTypeService.createType(高熔点金属.getId(), "镓");
			goodTypeService.createType(高熔点金属.getId(), "铟");
			goodTypeService.createType(高熔点金属.getId(), "铊");
			goodTypeService.createType(高熔点金属.getId(), "铌");

			goodTypeService.createType(放射性金属.getId(), "锗");
			goodTypeService.createType(放射性金属.getId(), "硒");
			goodTypeService.createType(放射性金属.getId(), "碲");
			goodTypeService.createType(放射性金属.getId(), "镧");
			goodTypeService.createType(放射性金属.getId(), "铈");
			goodTypeService.createType(放射性金属.getId(), "镨");
			goodTypeService.createType(放射性金属.getId(), "钕");
			goodTypeService.createType(放射性金属.getId(), "钷");
			goodTypeService.createType(放射性金属.getId(), "钐");

			goodTypeService.createType(稀散金属.getId(), "铕");
			goodTypeService.createType(稀散金属.getId(), "钆");
			goodTypeService.createType(稀散金属.getId(), "铽");
			goodTypeService.createType(稀散金属.getId(), "镝");
			goodTypeService.createType(稀散金属.getId(), "钐");
			goodTypeService.createType(稀散金属.getId(), "钐");

			BeanType 黑色金属 = goodTypeService.createType(goodType.getId(), "黑色金属");

			BeanType 铁 = goodTypeService.createType(黑色金属.getId(), "铁");
			BeanType 锰 = goodTypeService.createType(黑色金属.getId(), "锰");
			BeanType 铬 = goodTypeService.createType(黑色金属.getId(), "铬");

			goodTypeService.createType(铁.getId(), "镁磁铁矿");
			goodTypeService.createType(铁.getId(), "镜铁矿");
			goodTypeService.createType(铁.getId(), "钛铁矿");
			goodTypeService.createType(铁.getId(), "钒铁矿");
			goodTypeService.createType(铁.getId(), "磁铁矿");
			goodTypeService.createType(铁.getId(), "褐铁矿");
			goodTypeService.createType(铁.getId(), "菱铁矿");
			goodTypeService.createType(铁.getId(), "硫化铁矿");
			goodTypeService.createType(铁.getId(), "铬铁矿");
			goodTypeService.createType(铁.getId(), "赤铁矿");
			goodTypeService.createType(铁.getId(), "钨铁矿");

			BeanType 非金属矿产 = goodTypeService.createType(goodType.getId(), "非金属矿产");

			BeanType 能源 = goodTypeService.createType(非金属矿产.getId(), "能源");
			BeanType 玻璃工业 = goodTypeService.createType(非金属矿产.getId(), "玻璃工业");
			BeanType 陶瓷工业 = goodTypeService.createType(非金属矿产.getId(), "陶瓷工业");
			BeanType 冶金工业 = goodTypeService.createType(非金属矿产.getId(), "冶金工业");

			BeanType 废金属 = goodTypeService.createType(goodType.getId(), "废金属");

			BeanType 水气矿产 = goodTypeService.createType(goodType.getId(), "水气矿产");

			BeanType 矿业设备及材料 = goodTypeService.createType(goodType.getId(), "矿业设备及材料");

			BeanType 矿业药剂 = goodTypeService.createType(goodType.getId(), "矿业药剂");

			BeanType 矿业书籍 = goodTypeService.createType(goodType.getId(), "矿业书籍");

			BeanType 办公用品 = goodTypeService.createType(goodType.getId(), "办公用品");
			BeanType 数码产品 = goodTypeService.createType(goodType.getId(), "数码产品");

			BeanType 钢材相关 = goodTypeService.createType(goodType.getId(), "钢材相关");

			BeanType 五金相关 = goodTypeService.createType(goodType.getId(), "五金相关");

			BeanType 矿用工具 = goodTypeService.createType(goodType.getId(), "矿用工具");

			BeanType 设计制作 = goodTypeService.createType(goodType.getId(), "设计制作");

		}
		if ("AD_ROOT".equals(rootName) && (goodType.getChildren() == null || goodType.getChildren().size() == 0)) {
			BeanType sy = goodTypeService.createType(goodType.getId(), "网站首页");
			Map<String, String> m1 = new HashMap<String, String>();
			m1.put("code", "i-1");
			m1.put("type", "index");
			sy.setProp(m1);
			goodTypeService.saveOrUpdate(sy, BeanType.class);
			// 为每个界面增加广告位
			BeanType ssy = goodTypeService.createType(goodType.getId(), "商城首页");
			Map<String, String> m2 = new HashMap<String, String>();
			m2.put("code", "i-2");
			m2.put("type", "sindex");
			ssy.setProp(m2);
			goodTypeService.saveOrUpdate(ssy, BeanType.class);
			List<BeanInfoModule> l = goodTypeService.findAll(BeanInfoModule.class);
			for (int i = 0; i < l.size(); i++) {
				BeanType a = goodTypeService.createType(goodType.getId(), l.get(i).getModulename());
				Map<String, String> m3 = new HashMap<String, String>();
				m3.put("code", l.get(i).getModuletype());
				m3.put("type", "md");
				a.setProp(m3);
				goodTypeService.saveOrUpdate(a, BeanType.class);
				List<BeanModuleColumn> cl = l.get(i).getModuleColumns();
				for (int j = 0; j < cl.size(); j++) {
					BeanType b = goodTypeService.createType(a.getId(), cl.get(j).getColumnname());
					Map<String, String> m4 = new HashMap<String, String>();
					m4.put("code", l.get(i).getModuletype() + "-" + cl.get(j).getColumntype());
					m4.put("type", "cl");
					b.setProp(m4);
					goodTypeService.saveOrUpdate(b, BeanType.class);

					BeanType cb = goodTypeService.createType(b.getId(), "内容");
					Map<String, String> m5 = new HashMap<String, String>();
					m5.put("code", l.get(i).getModuletype() + "-" + cl.get(j).getColumntype());
					m5.put("type", "co");
					cb.setProp(m5);
					goodTypeService.saveOrUpdate(a, BeanType.class);
				}
			}
		}
		if ("AREA_ROOT".equals(rootName) && (goodType.getChildren() == null || goodType.getChildren().size() == 0)) {
			for (int i = 0; i < 3; i++) {
				BeanType a = goodTypeService.createType(goodType.getId(), "云南" + i);
				goodTypeService.save(a);
				for (int j = 0; j < 2; j++) {
					BeanType b = goodTypeService.createType(a.getId(), a.getName() + " 市" + j);
					goodTypeService.save(b);
					for (int k = 0; k < 3; k++) {
						BeanType c = goodTypeService.createType(b.getId(), b.getName() + " 区" + k);
						goodTypeService.save(c);
					}
				}
			}
		}
		//----------------------------------------------------
		//针对第16个问题 进行过滤 矿业资源和及时信息不参与竟价
		//是否会影响到其它暂时不确定
		List<BeanType> list = new ArrayList<BeanType>();
		for (BeanType beanType : goodType.getChildren()) {
			if (beanType.getName().equals("矿业资源")) {
				
			} else if (beanType.getName().equals("及时信息")) {
				
			}else{
				list.add(beanType);
			}
		}
		goodType.setChildren(list);
		//针对第16个问题 进行过滤 矿业资源和及时信息不参与竟价
		//----------------------------------------------------
		tree = new JQueryTreeGoodTypeImpl(goodType);
		return "success";
	}

	@Resource
	public void setGoodTypeService(IBeanTypeService goodTypeService) {
		this.goodTypeService = goodTypeService;
	}

	/**
	 * @return the tree
	 */
	public JQueryTree getTree() {
		return tree;
	}

	/**
	 * @param tree
	 *            the tree to set
	 */
	public void setTree(JQueryTree tree) {
		this.tree = tree;
	}

	/**
	 * @return the typeName
	 */
	public String getRootName() {
		return rootName;
	}

	/**
	 * @param typeName
	 *            the typeName to set
	 */
	public void setRootName(String rootName) {
		this.rootName = rootName;
	}

}
